package com.carl.zlds.dao.sqlprovider;

import com.carl.zlds.pojo.dto.AdSearchDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.HashMap;

/**
 * @Author: carl
 * @Date: 2020/6/24 2:29 PM
 */
public class AdSqlProvider {
    public String getAdList(HashMap<String, Object> param) {
        AdSearchDTO dto = (AdSearchDTO) param.get("dto");
        String limitSql = (String) param.get("limitSql");
        StringBuilder sql = new StringBuilder();
        if (limitSql.isEmpty()) {
            sql.append(" select count(a.id) ");
        } else {
            sql.append(" select a.* ");
        }
        sql.append(" from ad a ");
        sql.append(" where a.weight >= 0 ");
        if (Strings.isNotEmpty(dto.getTitle())) {
            sql.append(" and a.title like concat('%', #{dto.title},'%') ");
        }
        if (dto.getType() != null &&  dto.getType() > 0) {
            sql.append(" and a.type = #{dto.type} ");
        }
        sql.append(" order by a.create_time desc ");

        System.out.println(sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY));
        return sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY);
    }

    public String getAdListByWeb(HashMap<String, Object> param) {
        AdSearchDTO dto = (AdSearchDTO) param.get("dto");
        StringBuilder sql = new StringBuilder();
        sql.append(" select a.* ");
        sql.append(" from ad a ");
        sql.append(" where a.weight >= 0 ");
        if (Strings.isNotEmpty(dto.getTitle())) {
            sql.append(" and a.title like concat('%', #{dto.title},'%') ");
        }
        if (dto.getType() != null &&  dto.getType() > 0) {
            sql.append(" and a.type = #{dto.type} ");
        }
        if (dto.getGameId() != null &&  dto.getGameId() > 0) {
            sql.append(" and a.game_id = #{dto.gameId} ");
        }
        sql.append(" order by a.create_time desc ");
        return sql.toString();
    }
}
